package com.github.lee.unsplash.global.interceptor

import com.github.lee.unsplash.manager.LoginManager
import okhttp3.Interceptor
import okhttp3.Response
import javax.inject.Inject

class HeaderInterceptor(
    private val lm: LoginManager,
    private val headers: MutableMap<String, String>
) : Interceptor {


    override fun intercept(chain: Interceptor.Chain): Response {
        return chain.proceed(chain.request().newBuilder().run {
            if (!headers.isNullOrEmpty()) {
                for (headMap in headers) {
                    headMap.value.let { addHeader(headMap.key, it).build() }
                }
            }
            if (lm.accountToken.isNotEmpty()) {
                addHeader("Authorization", "Bearer ${lm.accountToken}")
            }
            build()
        })
    }
}